//
//  YLAStroeCell.swift
//  Your Lover AI
//
//  Created by 阿忠 on 2024/6/26.
//

import Foundation
import UIKit
import SwiftyStoreKit

class YLAStroeCell: UITableViewCell {
    
    var princeStr = "lift"
    
    protocol CellDelegate: class {
        func cell(_ cell: YLAStroeCell, toher1: Any)
 
    }
    
    weak var delegate: CellDelegate?
    
    var lifeVc: YLAPriceView! = nil
    var yearVc: YLAPriceView! = nil
    var weekVc: YLAPriceView! = nil
    var explainView: YLAmemberView! = nil
    var yearView: YLAmemberView! = nil
    var weekView: YLAmemberView! = nil
    
    var lifeStr = "29.99"
    var yearStr = "19.99"
    var weekStr = "9.99"
    
    //顶部
    lazy var topVc: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        view.layer.cornerRadius = 15.0 // 设置圆角半径为10.0
        view.layer.masksToBounds = true
        return view
    }()
    
    //purchase
    lazy var purchaseLab: UILabel = {
        let lab = UILabel()
        lab.text = "Purchase (One-time payment,no need renew)"
        lab.textColor = .white
        lab.font = UIFont.systemFont(ofSize: 12)
        lab.textAlignment = .left
        lab.numberOfLines = 0
        return lab
    }()
    
    //简介
    lazy var subscriLab: UILabel = {
        let lab = UILabel()
        lab.text = "Subscription (Cancel renewal at any time)"
        lab.textColor = .white
        lab.font = UIFont.systemFont(ofSize: 12)
        lab.textAlignment = .left
        lab.numberOfLines = 0
        return lab
    }()
    
    //时间
    lazy var explainLab: UILabel = {
        let lab = UILabel()
        lab.text = "1、Your Lover AI Membership Weekly Package Instructions\nSubscription period: 1 week\nSubscription price: 9.99 per week\nPayment: After the user confirms the purchase and payment, it will be credited to the iTunes account\nCancel renewal: If you need to cancel the renewal, please manually turn it off in the iTunes/Apple ID settings management 24 hours before the current subscription expires\nAutomatic renewal function\nRenewal: Apple iTunes account will deduct the payment within 24 hours before expiration. After the deduction is successful, the subscription period will be extended by 1 week\n\n2、Your Lover AI Membership Yearly Package Instructions\nSubscription Period: 1 Year\nSubscription Price: 19.99  per Week\nPayment: After the user confirms the purchase and payment, it will be credited to the iTunes account\nCancel renewal: If you need to cancel the renewal, please manually turn it off in the iTunes/Apple ID settings management 24 hours before the current subscription expires\nAutomatic renewal function\nRenewal: Apple iTunes account will deduct the payment within 24 hours before the expiration date. After the deduction is successful, the subscription period will be extended by 1 year\n\n3、Your Lover AI Membership Buyout Instructions\nSubscription Period: Lifetime\nSubscription Price: 29.99 per week\nPayment: After the user confirms the purchase and payment, it will be credited to the iTunes account\nRenewal: In-app purchases are one-time consumption and do not require renewal"
        lab.textColor = .white
        lab.font = UIFont.systemFont(ofSize: 12)
        lab.textAlignment = .left
        lab.numberOfLines = 0
        return lab
    }()
    
    lazy var privacyBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = .clear
        btn.setTitle("Privacy Policy", for: .normal)
//        btn.layer.masksToBounds = true
//        btn.layer.cornerRadius = 25
        btn.setTitleColor(UIColor(hex: "FFE295"), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btn.addTarget(self, action: #selector(privacyClick), for: .touchUpInside)
        return btn
    }()
    
    lazy var termsBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = .clear
        btn.setTitle("Terms", for: .normal)
//        btn.layer.masksToBounds = true
//        btn.layer.cornerRadius = 25
        btn.setTitleColor(UIColor(hex: "FFE295"), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btn.addTarget(self, action: #selector(termsClick), for: .touchUpInside)
        return btn
    }()
    
    //简介
    lazy var andLab: UILabel = {
        let lab = UILabel()
        lab.text = "And"
        lab.textColor = .white
        lab.font = UIFont.systemFont(ofSize: 12)
        lab.textAlignment = .center
        lab.numberOfLines = 0
        return lab
    }()
    
    //恢复购买
    lazy var restorBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = .clear
        btn.setTitle("restoring purchase products", for: .normal)
//        btn.layer.masksToBounds = true
//        btn.layer.cornerRadius = 25
        btn.setTitleColor(UIColor(hex: "FFE295"), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        btn.addTarget(self, action: #selector(restorClick), for: .touchUpInside)
        return btn
    }()
    
    lazy var buyBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = UIColor(hex: "F4C478")
        btn.setTitle("Buy now", for: .normal)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 25
        btn.setTitleColor(UIColor(hex: "8B4C07"), for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        btn.addTarget(self, action: #selector(buyClick), for: .touchUpInside)
        return btn
    }()
    
    //终身
    lazy var lifeBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(lifeClick), for: .touchUpInside)
        return button
    }()
    
    //一年
    lazy var yearBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(yearClick), for: .touchUpInside)
        return button
    }()
    
    //一周
    lazy var weekBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(weekClick), for: .touchUpInside)
        return button
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .clear
        contentView.addSubview(topVc)
        contentView.addSubview(purchaseLab)
        contentView.addSubview(subscriLab)
        contentView.addSubview(explainLab)
        contentView.addSubview(buyBtn)
        
        //请求价格
        setPrice()
        
    }

    func setPrice(){
     
        //终生
        SwiftyStoreKit.retrieveProductsInfo([YLAPriceDate().Lift]) { result in
            if let product = result.retrievedProducts.first {
                let priceString = product.localizedPrice!
                self.lifeStr = product.price.stringValue
                print("Product: \(product.localizedDescription), price: \(priceString)")
            }
            else if let invalidProductId = result.invalidProductIDs.first {
                print("Invalid product identifier: \(invalidProductId)")
            }
            else {
                print("Error: \(result.error)")
            }
        }
        
        //年
        SwiftyStoreKit.retrieveProductsInfo([YLAPriceDate().Year]) { result in
            if let product = result.retrievedProducts.first {
                let priceString = product.localizedPrice!
                self.yearStr = product.price.stringValue
                print("Product: \(product.localizedDescription), price: \(priceString)")
            }
            else if let invalidProductId = result.invalidProductIDs.first {
                print("Invalid product identifier: \(invalidProductId)")
            }
            else {
                print("Error: \(result.error)")
            }
        }
        
        //周
        SwiftyStoreKit.retrieveProductsInfo([YLAPriceDate().Week]) { result in
            if let product = result.retrievedProducts.first {
                let priceString = product.localizedPrice!
                self.weekStr = product.price.stringValue
                print("Product: \(product.localizedDescription), price: \(priceString)")
            }
            else if let invalidProductId = result.invalidProductIDs.first {
                print("Invalid product identifier: \(invalidProductId)")
            }
            else {
                print("Error: \(result.error)")
            }
        }
        
        initUI()
        
    }
    

    
    
    func initUI()
    {
        topVc.snp.makeConstraints { make in
            make.left.equalTo(31)
            make.right.equalTo(-31)
            make.top.equalTo(0)
            make.height.equalTo(200)
        }
        
        purchaseLab.snp.makeConstraints { make in
            make.left.equalTo(16.5)
            make.right.equalTo(16.5)
            make.top.equalTo(topVc.snp.bottom).offset(16)
            make.height.equalTo(12)
        }
        
        let dialogFrame = CGRect(x: 0, y: 200, width: 270, height: 200)
        
        self.explainView = YLAmemberView(frame: dialogFrame, time: "Lifetime", content: "Billed once", price: self.lifeStr)
        self.explainView.backVc.backgroundColor = UIColor(hex: "F5BA5D")
        self.explainView.titleLabel.text = "Lifetime Rights Description"
        self.explainView.wirtVc.backgroundColor = UIColor(hex: "FFFFFF").withAlphaComponent(0.16)
        self.explainView.content1Label.text = "Time: lifelong"
        self.explainView.content2Label.text = "Have unlimited conversations"
        self.explainView.content3Label.text = "View pictures unlimited times"
        self.explainView.content4Label.text = "30 minutes of phone time per week"
        self.explainView.content5Label.text = "No ads"
        topVc.addSubview(self.explainView)
        self.explainView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
        
        self.yearView = YLAmemberView(frame: dialogFrame, time: "Lifetime", content: "Billed once", price: self.yearStr)
        self.yearView.backVc.backgroundColor = UIColor(hex: "5A83B4")
        self.yearView.titleLabel.text = "Year subscription benefits"
        self.yearView.wirtVc.backgroundColor = UIColor(hex: "FFFFFF").withAlphaComponent(0.1)
        self.yearView.content1Label.text = "Time: 1 Year"
        self.yearView.content2Label.text = "Have unlimited conversations"
        self.yearView.content3Label.text = "View pictures unlimited times"
        self.yearView.content4Label.text = "10 minutes of phone time per week"
        self.yearView.content5Label.text = "No ads"
        self.yearView.titleLabel.textColor = UIColor(hex: "FFFFFF")
        self.yearView.content1Label.textColor = UIColor(hex: "FFFFFF")
        self.yearView.content2Label.textColor = UIColor(hex: "FFFFFF")
        self.yearView.content3Label.textColor = UIColor(hex: "FFFFFF")
        self.yearView.content4Label.textColor = UIColor(hex: "FFFFFF")
        self.yearView.content5Label.textColor = UIColor(hex: "FFFFFF")
        
        
        topVc.addSubview(self.yearView)
        self.yearView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
        
        self.weekView = YLAmemberView(frame: dialogFrame, time: "Lifetime", content: "Billed once", price:  self.weekStr)
        self.weekView.backVc.backgroundColor = UIColor(hex: "8575C3")
        self.weekView.titleLabel.text = "Week subscription benefts"
        self.weekView.wirtVc.backgroundColor = UIColor(hex: "FFFFFF").withAlphaComponent(0.1)
        self.weekView.content1Label.text = "Time: 1 Week"
        self.weekView.content2Label.text = "There are 450 conversations"
        self.weekView.content3Label.text = "View pictures 3 times"
        self.weekView.content4Label.text = "5 minutes of phone time per week"
        self.weekView.content5Label.text = "No ads"
        self.weekView.titleLabel.textColor = UIColor(hex: "FFFFFF")
        self.weekView.content1Label.textColor = UIColor(hex: "FFFFFF")
        self.weekView.content2Label.textColor = UIColor(hex: "FFFFFF")
        self.weekView.content3Label.textColor = UIColor(hex: "FFFFFF")
        self.weekView.content4Label.textColor = UIColor(hex: "FFFFFF")
        self.weekView.content5Label.textColor = UIColor(hex: "FFFFFF")
        
        topVc.addSubview(self.weekView)
        self.weekView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
        
        self.lifeVc = YLAPriceView(frame: dialogFrame, time: "Lifetime", content: "Billed once", price: "$" + self.lifeStr)
        self.lifeVc.layer.borderColor = UIColor.white.cgColor
        self.lifeVc.layer.borderWidth = 2
        self.lifeVc.layer.cornerRadius = 15.0
        contentView.addSubview(self.lifeVc)
        self.lifeVc.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(purchaseLab.snp.bottom).offset(14)
            make.height.equalTo(71)
        }
        
        contentView.addSubview(lifeBtn)
        lifeBtn.snp.makeConstraints { make in
            make.left.top.right.bottom.equalTo(self.lifeVc)
        }
        
        subscriLab.snp.makeConstraints { make in
            make.left.equalTo(16.5)
            make.right.equalTo(-16.5)
            make.top.equalTo(self.lifeVc.snp.bottom).offset(14)
            make.height.equalTo(12)
        }
        
        self.yearVc = YLAPriceView(frame: dialogFrame, time: "1 Year", content: "Automatically renew every year", price: "$" + self.yearStr)
        self.yearVc.timeLabel.textColor = .white
        self.yearVc.contentLabel.textColor = .white
        self.yearVc.priceLabel.textColor = .white
        contentView.addSubview(self.yearVc)
        self.yearVc.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(subscriLab.snp.bottom).offset(14)
            make.height.equalTo(71)
        }
        
        contentView.addSubview(yearBtn)
        yearBtn.snp.makeConstraints { make in
            make.left.top.right.bottom.equalTo(self.yearVc)
        }
        
        self.weekVc = YLAPriceView(frame: dialogFrame, time: "1 Week", content: "Automatically renew every week", price: "$" + self.weekStr)
        self.weekVc.timeLabel.textColor = .white
        self.weekVc.contentLabel.textColor = .white
        self.weekVc.priceLabel.textColor = .white
        contentView.addSubview(self.weekVc)
        self.weekVc.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(self.yearVc.snp.bottom).offset(14)
            make.height.equalTo(71)
        }
        
        contentView.addSubview(weekBtn)
        weekBtn.snp.makeConstraints { make in
            make.left.top.right.bottom.equalTo(self.weekVc)
        }
        
        buyBtn.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(self.weekVc.snp.bottom).offset(18)
            make.height.equalTo(52)
        }
        
        explainLab.snp.makeConstraints { make in
            make.left.equalTo(16.5)
            make.right.equalTo(-16.5)
            make.top.equalTo(buyBtn.snp.bottom).offset(18)
        }

        contentView.addSubview(privacyBtn)
        privacyBtn.snp.makeConstraints { make in
            make.top.equalTo(explainLab.snp.bottom).offset(14)
            make.right.equalTo(contentView.snp.centerX)
        }
        
        contentView.addSubview(andLab)
        andLab.snp.makeConstraints { make in
            make.centerY.equalTo(privacyBtn)
            make.left.equalTo(privacyBtn.snp.right).offset(3)
        }
        
        contentView.addSubview(termsBtn)
        termsBtn.snp.makeConstraints { make in
            make.left.equalTo(andLab.snp.right).offset(3)
            make.centerY.equalTo(andLab)
        }
        
        contentView.addSubview(restorBtn)
        restorBtn.snp.makeConstraints { make in
            make.top.equalTo(andLab.snp.bottom).offset(3)
            make.centerX.equalToSuperview()
        }
        
        self.explainView.isHidden = false
        self.yearView.isHidden = true
        self.weekView.isHidden = true
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func buyClick() {
        print("----------------点击了会员购买-----------------")
        delegate?.cell(self, toher1: self.princeStr)
       
    }
    
    @objc func privacyClick() {
        print("----------------点击了协议-----------------")
        delegate?.cell(self, toher1: "privacy")
       
    }
    
    @objc func termsClick() {
        print("----------------点击了团队-----------------")
        delegate?.cell(self, toher1: "terms")
       
    }
    
    @objc func restorClick() {
        print("----------------点击了恢复-----------------")
        delegate?.cell(self, toher1: "renstor")
       
    }
    
    @objc func lifeClick(){
        print("----------------点击了终身购买-----------------")
        self.lifeVc.layer.borderColor = UIColor.white.cgColor
        self.lifeVc.layer.borderWidth = 2
        self.lifeVc.layer.cornerRadius = 15.0
        
        self.yearVc.layer.borderColor = UIColor.clear.cgColor
        self.yearVc.layer.borderWidth = 2
        self.yearVc.layer.cornerRadius = 15.0
        
        self.weekVc.layer.borderColor = UIColor.clear.cgColor
        self.weekVc.layer.borderWidth = 2
        self.weekVc.layer.cornerRadius = 15.0
        
        self.explainView.isHidden = false
        self.yearView.isHidden = true
        self.weekView.isHidden = true
        self.princeStr = "lift"
        
        let userDefaults = UserDefaults.standard
        userDefaults.set("life", forKey: "stroeType")
    }
    
    @objc func yearClick(){
        print("----------------点击了年购买-----------------")
        self.lifeVc.layer.borderColor = UIColor.clear.cgColor
        self.lifeVc.layer.borderWidth = 2
        self.lifeVc.layer.cornerRadius = 15.0
        
        self.yearVc.layer.borderColor = UIColor.white.cgColor
        self.yearVc.layer.borderWidth = 2
        self.yearVc.layer.cornerRadius = 15.0
        
        self.weekVc.layer.borderColor = UIColor.clear.cgColor
        self.weekVc.layer.borderWidth = 2
        self.weekVc.layer.cornerRadius = 15.0
        
        self.explainView.isHidden = true
        self.yearView.isHidden = false
        self.weekView.isHidden = true
        self.princeStr = "year"
        let userDefaults = UserDefaults.standard
        userDefaults.set("year", forKey: "stroeType")
    }
    
    @objc func weekClick(){
        print("----------------点击了周购买-----------------")
        self.lifeVc.layer.borderColor = UIColor.clear.cgColor
        self.lifeVc.layer.borderWidth = 2
        self.lifeVc.layer.cornerRadius = 15.0
        
        self.yearVc.layer.borderColor = UIColor.clear.cgColor
        self.yearVc.layer.borderWidth = 2
        self.yearVc.layer.cornerRadius = 15.0
        
        self.weekVc.layer.borderColor = UIColor.white.cgColor
        self.weekVc.layer.borderWidth = 2
        self.weekVc.layer.cornerRadius = 15.0
        
        self.explainView.isHidden = true
        self.yearView.isHidden = true
        self.weekView.isHidden = false
        self.princeStr = "week"
        let userDefaults = UserDefaults.standard
        userDefaults.set("week", forKey: "stroeType")
    }
}
